<div id="app">
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="买车时间" prop="buytime">
      <el-input v-model="dataForm.buytime" placeholder="买车时间"></el-input>
    </el-form-item>
    <el-form-item label="买车金额" prop="buymoney">
      <el-input v-model="dataForm.buymoney" placeholder="买车金额"></el-input>
    </el-form-item>
    <el-form-item label="期数" prop="plan">
      <el-input v-model="dataForm.plan" placeholder="期数"></el-input>
    </el-form-item>
    <el-form-item label="最近还款时间" prop="recentpayback">
      <el-input v-model="dataForm.recentpayback" placeholder="最近还款时间"></el-input>
    </el-form-item>
    <el-form-item label="已还本金" prop="paybackmoney">
      <el-input v-model="dataForm.paybackmoney" placeholder="已还本金"></el-input>
    </el-form-item>
    <el-form-item label="未还本金" prop="losepackbackmoney">
      <el-input v-model="dataForm.losepackbackmoney" placeholder="未还本金"></el-input>
    </el-form-item>
    <el-form-item label="利率" prop="addway">
      <el-input v-model="dataForm.addway" placeholder="利率"></el-input>
    </el-form-item>
    <el-form-item label="利息" prop="payadd">
      <el-input v-model="dataForm.payadd" placeholder="利息"></el-input>
    </el-form-item>
    <el-form-item label="放款机构" prop="address">
      <el-input v-model="dataForm.address" placeholder="放款机构"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</div>

<script>
  new Vue ({
    el:"#app",
    data () {
      return {
        baseUrl:"http://localhost:88/api",
        visible: false,
        dataForm: {
          id: 0,
          buytime: '',
          buymoney: '',
          plan: '',
          recentpayback: '',
          paybackmoney: '',
          losepackbackmoney: '',
          addway: '',
          payadd: '',
          address: '',
          status: ''
        },
        dataRule: {
          buytime: [
            { required: true, message: '买车时间不能为空', trigger: 'blur' }
          ],
          buymoney: [
            { required: true, message: '买车金额不能为空', trigger: 'blur' }
          ],
          plan: [
            { required: true, message: '期数不能为空', trigger: 'blur' }
          ],
          recentpayback: [
            { required: true, message: '最近还款时间不能为空', trigger: 'blur' }
          ],
          paybackmoney: [
            { required: true, message: '已还本金不能为空', trigger: 'blur' }
          ],
          losepackbackmoney: [
            { required: true, message: '未还本金不能为空', trigger: 'blur' }
          ],
          addway: [
            { required: true, message: '利率不能为空', trigger: 'blur' }
          ],
          payadd: [
            { required: true, message: '利息不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '放款机构不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            axios.post(this.baseUrl+`/car/record/info/${this.dataForm.id}`).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.buytime = data.record.buytime
                this.dataForm.buymoney = data.record.buymoney
                this.dataForm.plan = data.record.plan
                this.dataForm.recentpayback = data.record.recentpayback
                this.dataForm.paybackmoney = data.record.paybackmoney
                this.dataForm.losepackbackmoney = data.record.losepackbackmoney
                this.dataForm.addway = data.record.addway
                this.dataForm.payadd = data.record.payadd
                this.dataForm.address = data.record.address
                this.dataForm.status = data.record.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            axios.post(this.baseUrl+`/car/record/${!this.dataForm.id ? 'save' : 'update'}`,this.dataForm).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  })
</script>
